<template>
  <div class="app-container">
    <el-table
      style="width: 100%"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" width="95">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>

      <el-table-column label="角色" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.role_name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="手机号" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary">编辑</el-button>
          <el-button type="danger" @click="deleteHandler(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import {getList} from '@/api/table'
  import axios from 'axios';


  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        list: null,
        listLoading: true
      }
    },
    created() {
      this.fetchData()
    },
    methods: {

      deleteHandler(scope) {
        this.$confirm('此操作将永久删除该用户' + scope.row.username + ', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      async fetchData() {
        this.listLoading = true
        /*getList().then(response => {
          this.list = response.data.items
          this.listLoading = false
        })*/
        let url = 'https://cmsapi.52kfw.cn/api/private/v1/users?pagenum=1&pagesize=10';

        let info = await axios.get(url, {
          headers: {
            authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2NTI0NTg4MDMsImV4cCI6MTY1MjU0NTIwM30.LvuNuaAbl8Tbl-bASd3d7HwloJnjjsdy6YUoN4ur6FU',
          }
        });

        this.listLoading = false

        this.list = info.data.data.users;

        console.log(info);

      }
    }
  }
</script>
